/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   Module Name          : com.yaodian100.ec.category.entity.Category
   Module Description   :

   Date Created      : 2009/11/4
   Original Author   : ellen.li
   Team              : yaodian100
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   MODIFICATION HISTORY
   ------------------------------------------------------------------------------
   Date Modified       Modified by       Comments
   ------------------------------------------------------------------------------
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
package com.yaodian100.ec.category.entity;

import java.math.BigDecimal;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

import org.apache.commons.lang.StringUtils;

import com.yaodian100.core.common.entity.BaseEntity;

/**
 * @author ellen.li
 * 
 */
@Entity
@Table(name = "EC_CATEGORY_MAIN")
public class Category extends BaseEntity {

	private static final char CATEGORY_PATH_SEPARATOR = '/';

	/* serialVersionUID */
	private static final long serialVersionUID = -3808543152438398055L;

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO, generator = "seq_ec_category")
	@SequenceGenerator(name = "seq_ec_category", sequenceName = "SEQ_EC_CATEGORY")
	@Column(name = "CATEGORY_ID")
	private Long id;

	@Column(name = "PARENT_ID", nullable = false)
	private Long parentId;

	@Column(name = "CATEGORY_NAME", length = 40)
	private String name;

	// 商品分類別名(用於前台部份區域呈現使用)
	@Column(name = "ALIAS_NAME", length = 40)
	private String aliasName;

	@Column(name = "TEMPLATE_ID")
	private String templateId;

	@Column(name = "START_DATE", nullable = false)
	@Temporal(TemporalType.TIMESTAMP)
	private Date startDate;

	@Column(name = "END_DATE", nullable = false)
	@Temporal(TemporalType.TIMESTAMP)
	private Date endDate;

	@Column(name = "SHOW_SEQ")
	private int showSeq;

	@Column(name = "IS_VISIBLE", length = 1)
	private String isVisible;

	@Column(name = "ORGNIZATION_PATH", length = 40)
	private String orgnizationPath;

	@Column(name = "PM_ID", length = 40)
	private String pmID;

	@Column(name = "CATEGORY_LEVEL", length = 5)
	private String level;

	@Column(name = "IS_ENDNODE", length = 1)
	private String isEndnode;

	@Column(name = "CATEGORY_PATH", length = 100)
	private String categoryPath;

	// 控制該分類是否被設成"活動推荐" (即分類頁左側目錄樹最上方之活動區塊)
	@Column(name = "IS_PROMOTION", length = 1)
	private String isPromotion;

	// 控制該分類名稱之字型是否被設成特殊顯示 (像是分類名稱為粗體紅字者, 即由此flag控制)
	@Column(name = "IS_HIGHLIGHT", length = 1)
	private String isHighlight;

	// 控三剛該分類是否設成"品牌分類" (即分類頁左側目錄樹最下方之品牌區塊)
	@Column(name = "IS_BRAND", length = 1)
	private String isBrand;

	@Column(name = "GROSS_PROFIT", columnDefinition = "NUMERIC(10, 2)")
	private BigDecimal grossProfit;

	/**
	 * 是否已刪除,default=N
	 */
	@Column(name = "IS_DELETE", columnDefinition = "VARCHAR2(2 CHAR) default 'N'")
	private String isDelete;

	/** default constructor */
	public Category() {
	}

	/**
	 * @return the aliasName
	 */
	public String getAliasName() {
		return aliasName;
	}

	/**
	 * @return the categoryPath
	 */
	public String getCategoryPath() {
		return categoryPath;
	}

	public Date getEndDate() {
		return endDate;
	}

	public Long getId() {
		return id;
	}

	public String getIsBrand() {
		return isBrand;
	}

	public String getIsEndnode() {
		return isEndnode;
	}

	/**
	 * @return the isHighlight
	 */
	public String getIsHighlight() {
		return isHighlight;
	}

	/**
	 * @return the isPromotion
	 */
	public String getIsPromotion() {
		return isPromotion;
	}

	public String getIsVisible() {
		return isVisible;
	}

	public String getLevel() {
		return level;
	}

	public String getName() {
		return name;
	}

	public String getOrgnizationPath() {
		return orgnizationPath;
	}

	public Long getParentId() {
		return parentId;
	}

	public String getPmID() {
		return pmID;
	}

	public int getShowSeq() {
		return showSeq;
	}

	public Date getStartDate() {
		return startDate;
	}

	public String getTemplateId() {
		return templateId;
	}

	/**
	 * 
	 * @return 回傳所屬館的 category id
	 */
	public String getTopParentCategoryId() {
		String categoryPath = getCategoryPath();

		if (categoryPath == null) {
			return null;
		}

		String[] categoryPathArray = StringUtils.split(categoryPath, CATEGORY_PATH_SEPARATOR);

		if (categoryPathArray.length <= 0) {
			return null;
		}

		return categoryPathArray[0];
	}

	/**
	 * @param aliasName the aliasName to set
	 */
	public void setAliasName(String aliasName) {
		this.aliasName = aliasName;
	}

	/**
	 * @param categoryPath the categoryPath to set
	 */
	public void setCategoryPath(String categoryPath) {
		this.categoryPath = categoryPath;
	}

	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public void setIsBrand(String isBrand) {
		this.isBrand = isBrand;
	}

	public void setIsEndnode(String isEndnode) {
		this.isEndnode = isEndnode;
	}

	/**
	 * @param isHighlight the isHighlight to set
	 */
	public void setIsHighlight(String isHighlight) {
		this.isHighlight = isHighlight;
	}

	/**
	 * @param isPromotion the isPromotion to set
	 */
	public void setIsPromotion(String isPromotion) {
		this.isPromotion = isPromotion;
	}

	public void setIsVisible(String isVisible) {
		this.isVisible = isVisible;
	}

	public void setLevel(String level) {
		this.level = level;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setOrgnizationPath(String orgnizationPath) {
		this.orgnizationPath = orgnizationPath;
	}

	public void setParentId(Long parentId) {
		this.parentId = parentId;
	}

	public void setPmID(String pmID) {
		this.pmID = pmID;
	}

	public void setShowSeq(int showSeq) {
		this.showSeq = showSeq;
	}

	public void setStartDate(Date startDate) {
		this.startDate = startDate;
	}

	public void setTemplateId(String templateId) {
		this.templateId = templateId;
	}

	public String getIsDelete() {
		return isDelete;
	}

	public void setIsDelete(String isDelete) {
		this.isDelete = isDelete;
	}

	@Transient
	public String getRootId() {
		String rootId = "";
		if (!StringUtils.equals("0", level) && StringUtils.isNotBlank(categoryPath)) {
			rootId = StringUtils.split(categoryPath, "/")[0];
		}
		return rootId;
	}

	public BigDecimal getGrossProfit() {
		return grossProfit;
	}

	public void setGrossProfit(BigDecimal grossProfit) {
		this.grossProfit = grossProfit;
	}
}